package com.coolweather.android;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.widget.TextView;


import androidx.appcompat.app.AppCompatActivity;

import com.coolweather.android.gson.Weather;
import com.google.gson.Gson;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class WeatherActivity extends AppCompatActivity {

    private String mWeatherId;
    private TextView txtCity;
    private Weather weatherObj;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_weathder);

        mWeatherId=getIntent().getStringExtra("weather_id");
        // Log.d("weather",mWeatherId);
        txtCity=findViewById(R.id.textView2);
        String weatherString=" ";
        String address="http://guolin.tech/api/weather?cityid="+mWeatherId+"&key=bc0418b57b2d4918819d3974ac1285d9";
        SharedPreferences prefs=PreferenceManager.getDefaultSharedPreferences(WeatherActivity.this);
        weatherString =prefs.getString("weather",null);
        if (weatherString.equals(" ")){
            queryDataFromServer(address);
        }else {
            try {
                disWeather(weatherString);
                txtCity.setText(weatherObj.basic.cityName);
            }catch (Exception e){
                e.printStackTrace();
            }
        }

    }

    private void disWeather(String weatherString) throws JSONException {
        parseWeather(weatherString);
    }

    private void queryDataFromServer(final String address){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    OkHttpClient client = new OkHttpClient();
                    Request request = new Request.Builder().url(address).build();
                    Response response = null;
                    response = client.newCall(request).execute();
                    String responseData = response.body().string();
                    Log.d("responseData", responseData);
                    SharedPreferences.Editor editor= PreferenceManager.getDefaultSharedPreferences(WeatherActivity.this).edit();
                    editor.putString("weather",responseData);
                    editor.apply();

                    SharedPreferences prefs=PreferenceManager.getDefaultSharedPreferences(WeatherActivity.this);
                    String weatherString=prefs.getString("weather",null);
                    Log.d("SharedPreferences",weatherString);
                    parseWeather(responseData);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }).start();
    }
    private void parseWeather(String responseDate) throws JSONException {
        JSONObject fistLevfel = new JSONObject(responseDate);//第一
        JSONArray jsonArray=fistLevfel.getJSONArray("HeWeather");
        String weatherContent=jsonArray.getJSONObject(0).toString();
        Log.d("parseWeatherData",weatherContent);
        //Weather weather= new Gson().fromJson(weatherContent,Weather.class);
        weatherObj= new Gson().fromJson(weatherContent, Weather.class);
        Log.d("parseWeatherData",weatherObj.basic.cityName);

    }

}